<?php
class Donate_Model extends CI_Model {
	function __construct() {
		parent::__construct();
	}

	/**
	 * 建立资助关系
	 * 学生状态改为2 - 建立关系
	 *
	 * @access public
	 * @param int 资助人ID
	 * @param int 学生ID
	 * @return int 返回状态码，成功返回0，数据库错误返回-1，学生状态错误返回1
	 */
	function create_donation($hlp_id, $stu_id) {
		$query = $this->db->query("SELECT stu_status FROM wb_relation WHERE stu_id = ". $stu_id);
		if($query->num_rows() > 0) {
			$res = $query->row_array();
			if($res['stu_status'] == 1) {
				$query = $this->db->query("UPDATE wb_relation SET hlp_id = ". $hlp_id. "AND stu_status = 2 WHERE stu_id = ". $stu_id);
				if($query == TRUE) {
					return 0;
				}
				else return -1;
			}
			else return 1;
		}
		else return -1;		
	}
	/**
	 * 解除资助关系
	 * 学生状态该为5 - 解除关系
	 *
	 * @access public
	 * @param int 资助人ID
	 * @param int 学生ID
	 * @return int 返回状态码，成功返回0，数据库错误返回-1，学生状态错误返回1
	 */
	function cancel_donation($hlp_id, $stu_id) {
		$query = $this->db->query("SELECT stu_status, hlp_id FROM wb_relation WHERE stu_id = ". $stu_id);
		if($query->num_rows() > 0 {
			$res = $query->row_array();
			if($res['stu_status'] == 4 && $res['hlp_id'] != 0) {
				$query = $this->db->query("UPDATE wb_relation SET hlp_id = 0 AND stu_status = 5 WHERE stu_id = ". $stu_id);
				if($query == TRUE) {
					return 0;
				}
				else return -1;
			}
			else return 1;
		}
		else return -1;
	}

	
	function remit($hlp_id, $stu_id, $amount, $purpose) {
		$query = $this->db->query("INSERT INTO wb_remit");
		if($query == TRUE) return 0;
		else return -1;
	}

	function get_remit_record($stu_id) {
		$query = $this->db->query("SELECT * FROM wb_remit WHERE stu_id = ".$stu_id);
		if($query->num_rows() > 0 {
			$item = array();
			foreach($query->result_array() as $row) {
				$item [] = $row;
			}
			return $item;
		}
		else return array();
	}

	function get_remit_count($stu_id) {
		$query = $this->db->query("SELECT COUNT(id) as count FROM wb_remit WHERE stu_id = ".$stu_id);
		if($query->num_rows() > 0) {
			$res = $query->row_array();
			return $res['count'];
		}
		else return -1;
	}

	function get_remit_sum($stu_id) {
		$query = $this->db->query("SELECT SUM(amount) as sum FROM wb_remit WHERE stu_id = ".$stu_id);
		if($query->num_rows() > 0) {
			$res = $query->row_array();
			return $res['sum'];
		}
		else return -1;
	}

	function feedback($hlp_id, $stu_id, $content) {

	}
	
	function reply_feedback($id, $in_charge, $reply) {

	}
}
/* End of file donate_model.php */
